#!/usr/bin/python
# -*- coding: cp1250 -*-

import string
import shutil
import glob
import re
import sys

# removing dependencies from pepe library in this case
# because this is good demonstration file for python beginners
# they are to be able to run it as standalone script 

#import pepe.p_txt.bigtxt as bt

class lines:
    ''' read big text file as iterator
    '''
    def __init__(self , flpath_s):
        self.flpath_s = flpath_s
        self.sfile_fl = open(flpath_s, 'r') 
    def __getitem__( self , index ):
        line_s = self.sfile_fl.readline()
        if line_s:
            return line_s
        else:
            raise IndexError

def findcoldelimiter_line(line):
    ''' guess what text file column delimiter is used 

    if cd == '': cd = findcoldelimiter_line(line)

    >>> findcoldelimiter_line("a|^b;^c~^d~")
    '^'
    '''
    c = 0; r = '' 
    for cs in [',', '\t', ';', '^', '~', '|']:
        if string.count(line, cs) > c:
            c = string.count(line, cs); r = cs
    return r

def main(filesmask):
    """ Trying to repair rows by counting total number of column delimiters present.
    
    2010-09-22_1357 vp initial version
    """
    for inpfile in glob.glob(filesmask):
    
        fname = string.split(inpfile, '.')[0]
    
        o = open(fname + '_repair_row.#', 'w')
        r = open(fname + '_repair_row_log.#', 'w')
    
        buffer_line = ''
    
        lino = 0
        len_cols = 0
    
        cd = ''
    
        bigest_line = 0
        biggest_column = 0
    
        #for line in bt.lines(inpfile):
        for line in lines(inpfile):
        
            #if len(line) > bigest_line: bigest_line = len(line)
    
            if cd == '': cd = findcoldelimiter_line(line)
        
            lino += 1
            line = re.sub('\n', '', line)
            # remove tabs in pipe delimited text file
            if cd <> '\t': line = re.sub('\t', ' ', line)
            
            cols = string.split(line, cd)
            #for c in cols:
            #    if len(c) > biggest_column: biggest_column = len(c)
    
            #print len(cols)
            # take header len cols as reference
            if len_cols == 0: len_cols = len(cols)
    
            if len(cols) == len_cols:
                o.write(line + '\n')
            else:
                if buffer_line == '':
                    buffer_line = line
                else:
                    print lino, 'repaired line'
                    r.write("\n\n%s %s\n%s" % (lino, 'repaired line', line))
                    cols_now = len(string.split(buffer_line + line, cd))
                    if cols_now == len_cols:
                        o.write(buffer_line + line + '\n')
                        buffer_line = ''
    
        #print bigest_line
        #print biggest_column
        
        o.close()
        r.close()

if __name__ == '__main__':
    ''' '''
    if    len(sys.argv) == 2:
        main(sys.argv[1])
    else:
        print """

        repair_rows <filemask>

        example : repair_rows *.dat

        filemask (mandatory)
            data file(s) to be processed

        """
